package com.qf.ayc.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.ayc.foster.entity.Foster;
import com.qf.ayc.orders.entity.Orders;
import com.qf.ayc.product.entity.Product;
import com.qf.ayc.product.mapper.ProductMapper;
import com.qf.ayc.product.service.IProductService;
import com.qf.ayc.user.entity.Users;
import com.qf.ayc.utils.ResultVo;
import com.qf.ayc.vips.entity.Vips;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaozhi
 * @since 2024-02-20
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {
   @Autowired
    private IProductService productService;

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductService iProductService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody Product product){

        // 查询所有数据 product表
        // select * from product ;
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();

        List<Product> products = productMapper.selectList(null);

        productService.add(product);

        return ResultVo.ok("200","添加成功");
    }

    @PostMapping("/delete")
    public ResultVo delete(@RequestBody Product product){
        productService.delete(product.getId());
        return ResultVo.ok("200","删除成功");
    }

    @PostMapping("/select")
    public ResultVo select(@RequestBody Product product){

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq(product.getProname()!=null,"proname",product.getProname())
                        .eq(product.getId()!=null,"id",product.getId())
                                .eq(product.getType()!=null,"type",product.getType());

        List<Product> data = productService.list(wrapper);

        List<Product> list = productService.list(); // 茶所有

        return  ResultVo.ok("200","查询成功",data);
    }


    @PostMapping("/search")
    public ResultVo search(@RequestBody Product product){
//        QueryWrapper<Product> wrapper = new QueryWrapper<>();
//        wrapper.eq(product.getProname()!=null,"proname",product.getProname());

        List<Product> data=productService.searchlist(product);

        return ResultVo.ok("200","查询成功",data);
    }
//    @PostMapping("/search")
//    public ResultVo search(@RequestBody Product product){
//          QueryWrapper<Product> wrapper = new QueryWrapper<>();
//          wrapper.eq(product.getProname()!=null,"proname",product.getProname());
//          List<Product> data=productService.searchlist(product);
//        return ResultVo.ok("200","查询成功",data);
//
//    }



    @PostMapping("/update")
   public  ResultVo update(@RequestBody Product product){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq(product.getProname()!=null,"id",product.getId());
        productMapper.update(product,wrapper);
        return ResultVo.ok("200","修改",product);
    }

    @GetMapping("/prolist")
    public ResultVo productlist(){
        List<Product> orderlist = productService.List();
        return ResultVo.ok("200", "所有商品信息", orderlist);
    }

//    分页
    @GetMapping("/productlist")
    public ResultVo getlist(Integer pageNum,Integer limit){
        List<Product> alllist=productService.getlist(pageNum,limit);
        return  ResultVo.ok("1","所有商品分页",alllist);
    }

    @PostMapping("/productCategory")
    public ResultVo select(String type){
        //1.打印日志
        //2.进行必要的参数校验
        //3.通过service层的对象进行业务处理
        List<Product> datalist = productService.select(type);
        return ResultVo.ok("200","查询成功",datalist);
    }
    //选择图片的第一次上传，这个功能无论是postman，接口文档调试，都需要在阿里云服务器上进行，本地不能连接服务器
    @PostMapping("/uppro")
    private String uppro(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "图片为空，请选择图片";
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 8);

        String fileName = uuid+file.getOriginalFilename()  ;
        // 上传文件路径的处理，相对项目jar包的部署路径存放upload目录
        String filePath = "/opt/proimg/";
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String dest = filePath + File.separator + fileName;
        log.debug("dest={}", dest);
        try {
            file.transferTo(new File(dest));
            log.info("上传成功");
            return  fileName;
        } catch (IOException e) {
            log.error(e.toString(), e);
        }
        return "图片修改失败";
    }
    //展示商品图片接口，要把这个接口完整链接拼接在img标签src属性里面上:src="'http://182.92.162.156:8083/users/downattr?filename=' + $store.state.user.user.attr"
    @GetMapping("/downpropic")
    public void download(String filename, HttpServletResponse response) throws IOException {
        String realPath = "/opt/proimg/";
        IOUtils.copy(new FileInputStream(realPath + filename), response.getOutputStream());
    }

}
